/*
 * Created on 20/07/2005
 *
 */
package br.uece.tcc.flathead.jxta.message;

import net.jxta.document.AdvertisementFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.StructuredTextDocument;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.protocol.PipeAdvertisement;
import br.uece.tcc.flathead.jxta.controller.FhJXTAController;
import br.uece.tcc.flathead.jxta.utils.Constantes;


/**
 * @author pedro
 *
 */
public class Mensagem {
    private Message message;
    
    private String tipoMsg = Constantes.MENSAGEM;

    public Mensagem() {
        this.message = new Message();
        setID(System.currentTimeMillis());
        setPipeAdv(FhJXTAController.getInstance().getFHJxtaBase().getInputPipe().getAdvertisement());
    }

    public Mensagem(Message msg) {
        this.message = msg;
    }

    public Mensagem(PipeAdvertisement pipeAdv, String peerOrigem, String mensagem) {
        this.message = new Message();
        setID(System.currentTimeMillis());
        setPipeAdv(pipeAdv);
        setPeerOrigem(peerOrigem);
    }

    private void removeElement(String tag) {
        MessageElement me = getMessage().getMessageElement(tag);
        getMessage().removeMessageElement(me);
    }

    /**
     * @return Returns the PipeAdvertisement.
     */
    public PipeAdvertisement getPipeAdv() {
        PipeAdvertisement adv = null;
        try {
            MessageElement contentsElement = getMessage().getMessageElement(Constantes.PIPE_ADV);
            StructuredTextDocument contentsDocument = (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument(new MimeMediaType("text/xml"), contentsElement.getStream());
            adv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(new MimeMediaType("text/xml"), contentsDocument.getStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return adv;
    }

    /**
     * @param pipeAdv
     *            The PipeAdvertisement to set.
     */
    public void setPipeAdv(PipeAdvertisement pipeAdv) {
        StructuredTextDocument advDoc = (StructuredTextDocument) pipeAdv.getDocument(new MimeMediaType("text/xml"));
        StringMessageElement contentsElement = new StringMessageElement(Constantes.PIPE_ADV, advDoc.toString(), null);
        getMessage().addMessageElement(contentsElement);
    }

    /**
     * @return Returns the peer.
     */
    public String getPeerOrigem() {
        MessageElement me = getMessage().getMessageElement(Constantes.PEER_ORIGEM);
        if (me == null)
            return "";
        return me.toString();
    }

    /**
     * @param peer
     *            O peer que criou a mensagem
     */
    public void setPeerOrigem(String peer) {
        removeElement(Constantes.PEER_ORIGEM);
        StringMessageElement sme = new StringMessageElement(Constantes.PEER_ORIGEM, peer, null);
        getMessage().addMessageElement(null, sme);
    }

    /**
     * @return Returns the message.
     */
    public String getMensagem() {
        MessageElement me = getMessage().getMessageElement(tipoMsg);
        if (me == null)
            return "";
        return me.toString();
    }

    /**
     * @param peer
     *            A mensagem a ser transmitida
     */
    public void setMensagem(String msg) {
        removeElement(tipoMsg);
        StringMessageElement sme = new StringMessageElement(tipoMsg, msg, null);
        getMessage().addMessageElement(null, sme);
    }
    /**
     * @return Returns the iD.
     */
    public long getID() {
        MessageElement me = getMessage().getMessageElement(Constantes.ID);
        return new Long(me.toString()).longValue();
    }

    public void setID(long id) {
        removeElement(Constantes.ID);
        StringMessageElement sme = new StringMessageElement(Constantes.ID, id + "", null);
        getMessage().addMessageElement(null, sme);
    }

    /**
     * @return Returns the message.
     */
    public Message getMessage() {
        return message;
    }

	public String getTipoMsg() {
		return tipoMsg;
	}

	public void setTipoMsg(String tipoMsg) {
		this.tipoMsg = tipoMsg;
	}
}